package com.b2c.ftysys.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.b2c.ftysys.domain.B2cFsyWip;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.FtyWipExample;
import com.b2c.ftysys.domain.PageResult;
import com.b2c.ftysys.domain.Product;
import com.b2c.ftysys.helper.ObjectUtils;
import com.b2c.ftysys.helper.ProductUtils;
import com.b2c.ftysys.sevice.ProductService;
import com.b2c.ftysys.sevice.RouteService;
import com.b2c.ftysys.sevice.WipService;
import com.b2c.ftysys.vo.FtyWipVo;

@Controller
public class WipAction extends MultiActionController {

	@Autowired
	RouteService routeService = null;

	@Autowired
	WipService wipService = null;

	@Autowired
	ProductService productService = null;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping("wip")
	public String toWipPage(HttpServletRequest request) {
		String proSku = request.getParameter("proId");
		request.setAttribute("proSku", proSku == null ? "" : proSku);
		if (null == proSku) {
			request.setAttribute("product", new Product());
		} else {
			request.setAttribute("product", productService
					.getProductByFtySku(proSku));

			String suggest = wipService.getSuggestRouteType(ProductUtils
					.getStyleNoByFtySku(proSku));
			if (!StringUtils.isEmpty(suggest))
				request.setAttribute("suggest", this.getMessage(
						"route.suggest", new Object[] { suggest }));
		}
		request.setAttribute("routeTypes", routeService.getAllRouteType());
		return "product/wip";
	}

	@RequestMapping(method = { RequestMethod.POST, RequestMethod.GET }, value = "orderWip")
	public String toOrderFtyPage(FtyWipVo vo, HttpServletRequest request)
			throws Exception {
		if (vo == null) {
			vo = (FtyWipVo) ObjectUtils.ConvertURLRequsetMapToObj(request
					.getParameterMap(), FtyWipVo.class);
		}
		
		FtyWipExample example = ExampleFactory.createFtyWipExample();
		example.createCriteria(vo);
		PageResult page = new PageResult();
		page.setSqlParamObj(example);
		if (null != vo.getCurPage() && 0 != vo.getCurPage()) {
			page.setCurPage(vo.getCurPage());
		}
		if (null != vo.getPageSize() && 0 != vo.getPageSize()) {
			page.setPageSize(vo.getPageSize());
		} else
			page.setPageSize(200);
		example.setOrderByClause(" trackNo,pdtSkuId ");
		request.setAttribute("vo", vo);
		request.setAttribute("dataList", this.wipService.getWipByPage(page));
		return "order/orderWip";
	}

	@RequestMapping(method = RequestMethod.POST, value = "wip")
	public String wipReg(HttpServletRequest request) {
		String[] wsId = request.getParameterValues("wsId");
		String[] staffId = request.getParameterValues("staffId");

		String proSku = request.getParameter("proId");
		String routeType = request.getParameter("routeType");
		String styleNo = request.getParameter("styleNo");

		if (StringUtils.isEmpty(routeType))
			throw new RuntimeException("未指定路由");

		B2cFsyWip wip = null;
		String mess = "";
		String errorMess = "";
		boolean first = true;
		for (int i = 0; i < wsId.length; i++) {
			wip = new B2cFsyWip();
			wip.setWsId(Integer.parseInt(wsId[i]));
			wip.setProSku(proSku);
			wip.setProStyleNo(styleNo);
			wip.setWsName(routeType);

			if (StringUtils.isEmpty(staffId[i])
					|| Integer.parseInt(staffId[i]) == 0)
				continue;

			wip.setStaffId(Integer.parseInt(staffId[i]));
			wip.setCreatorId(wip.getStaffId());
			int num = wipService.regWip(wip, routeType);

			if (first) {
				if (num == 1) {
					mess += (i + 1);
				} else if (num == -1) {
					errorMess += "[工序" + (i + 1) + "添加失败，原因:此产品已执行完毕]";
				}
				first = false;
			} else {
				if (num == 1) {
					mess += (i + 1);
				} else if (num == -1) {
					errorMess += "[工序" + (i + 1) + "添加失败，原因:此产品已执行完毕]";
				}
			}
		}

		if (!first)
			wipService.clearAllCache();

		request.setAttribute("proSku", proSku);
		request.setAttribute("mess", mess);
		request.setAttribute("errorMess", errorMess);

		return "product/wipReg";
	}

	protected final String getMessage(String msgKey) {
		return super.getMessageSourceAccessor().getMessage(msgKey);
	}

	protected String getMessage(String msgKey, Object[] args) {
		return super.getMessageSourceAccessor().getMessage(msgKey, args);
	}
}
